<?php
declare (strict_types = 1);

namespace app\common\model\mysql;

/**
 * @mixin \think\Model
 */
class Address extends BaseModel
{
    /**
     * 电话修改器
     *
     * @param string $value
     * @return array|string|string[]
     */
    public function getPhoneAttr(string $value)
    {
        return $value ? substr_replace($value, '****', 3, 4) : $value;
    }

    /**
     * 手机修改器
     *
     * @param string $value
     * @return array|string|string[]
     */
    public function getMobileAttr(string $value)
    {
        return $value ? substr_replace($value, '****', 3, 4) : $value;
    }

    /**
     * 查找符合条件的所有正常数据
     *
     * @param array $condition
     * @return Address|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalAllDataByCondition(array $condition = [])
    {
        $order = ['is_default' => 'desc', 'id' => 'desc'];
        $res = $condition ? $this->where($condition) : $this;
        $result = $res->where('status', config('status.mysql.table_normal'))->order($order)->select();
        return $result;
    }

    /**
     * 查找符合条件的正常数据
     *
     * @param array $condition
     * @return Address|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalDataByCondition(array $condition = [])
    {
        $res = $condition ? $this->where($condition) : $this;
        $result = $res->where('status', config('status.mysql.table_normal'))->find();
        return $result;
    }


}
